<?php
include_once dirname(__FILE__)."/../../model/config.php";
		$_unite_email=new unite_email();
if(isset($_POST["operation"])){
if($_POST["operation"]=="INSERT"){
		if(isset($_POST["id"])){$_unite_email->set_id($_POST["id"]);}
		if(isset($_POST["id_unite"])){$_unite_email->set_id_unite($_POST["id_unite"]);}
		if(isset($_POST["id_mail"])){$_unite_email->set_id_mail($_POST["id_mail"]);}
		if(isset($_POST["id_config_envoi"])){$_unite_email->set_id_config_envoi($_POST["id_config_envoi"]);}
		if(isset($_POST["phase"])){$_unite_email->set_phase($_POST["phase"]);}
		if(isset($_POST["envoyer"])){$_unite_email->set_envoyer($_POST["envoyer"]);}
		if(isset($_POST["erreur"])){$_unite_email->set_erreur($_POST["erreur"]);}
		if(isset($_POST["mail"])){$_unite_email->set_mail($_POST["mail"]);}
		if(isset($_POST["email"])){$_unite_email->set_email($_POST["email"]);}
		if(isset($_POST["personne"])){$_unite_email->set_personne($_POST["personne"]);}
		if(isset($_POST["sujet"])){$_unite_email->set_sujet($_POST["sujet"]);}
		$res=$_unite_email->INSERT(); 

	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('success' => 'true','result' => "$res" ));
	  	
	  }
}
elseif($_POST["operation"]=="DELETE"){
		if(isset($_POST["id"])){
		$_unite_email->set_id($_POST["id"]);
		$res=$_unite_email->DELETE(); 

	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('success' => 'true','result' => "$res" ));
	  	
	  }
		}
}
elseif($_POST["operation"]=="UPDATE"){
		if(isset($_POST["id"])){$_unite_email->set_id($_POST["id"]);}
		if(isset($_POST["id_unite"])){$_unite_email->set_id_unite($_POST["id_unite"]);}
		if(isset($_POST["id_mail"])){$_unite_email->set_id_mail($_POST["id_mail"]);}
		if(isset($_POST["id_config_envoi"])){$_unite_email->set_id_config_envoi($_POST["id_config_envoi"]);}
		if(isset($_POST["phase"])){$_unite_email->set_phase($_POST["phase"]);}
		if(isset($_POST["envoyer"])){$_unite_email->set_envoyer($_POST["envoyer"]);}
		if(isset($_POST["erreur"])){$_unite_email->set_erreur($_POST["erreur"]);}
		if(isset($_POST["mail"])){$_unite_email->set_mail($_POST["mail"]);}
		if(isset($_POST["email"])){$_unite_email->set_email($_POST["email"]);}
		if(isset($_POST["personne"])){$_unite_email->set_personne($_POST["personne"]);}
		if(isset($_POST["sujet"])){$_unite_email->set_sujet($_POST["sujet"]);}
		$res=$_unite_email->UPDATE(); 

	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('success' => 'true','result' => "$res" ));
	  	
	  }
}

}
else{
	$constraint='';$limit=0;$start=0;$pagination='';
	if(isset($_POST["id"]) && !empty($_POST["id"])){
		$constraint.=" AND id='".$_POST["id"]."'";
	}

	if(isset($_POST["id_unite"]) && !empty($_POST["id_unite"])){
		$constraint.=" AND id_unite='".$_POST["id_unite"]."'";
	}

	if(isset($_POST["id_mail"]) && !empty($_POST["id_mail"])){
		$constraint.=" AND id_mail='".$_POST["id_mail"]."'";
	}

	if(isset($_POST["id_config_envoi"]) && !empty($_POST["id_config_envoi"])){
		$constraint.=" AND id_config_envoi='".$_POST["id_config_envoi"]."'";
	}

	if(isset($_POST["phase"]) && !empty($_POST["phase"])){
		$constraint.=" AND phase='".$_POST["phase"]."'";
	}

	if(isset($_POST["envoyer"]) && !empty($_POST["envoyer"])){
		$constraint.=" AND envoyer='".$_POST["envoyer"]."'";
	}

	if(isset($_POST["erreur"]) && !empty($_POST["erreur"])){
		$constraint.=" AND erreur='".$_POST["erreur"]."'";
	}

	if(isset($_POST["mail"]) && !empty($_POST["mail"])){
		$constraint.=" AND mail='".$_POST["mail"]."'";
	}

	if(isset($_POST["email"]) && !empty($_POST["email"])){
		$constraint.=" AND email='".$_POST["email"]."'";
	}

	if(isset($_POST["personne"]) && !empty($_POST["personne"])){
		$constraint.=" AND personne='".$_POST["personne"]."'";
	}

	if(isset($_POST["sujet"]) && !empty($_POST["sujet"])){
		$constraint.=" AND sujet='".$_POST["sujet"]."'";
	}

	if(isset($_POST['limit'])){$limit=$_POST['limit'];}
	if(isset($_POST['start'])){$start=$_POST['start'];}
	if($limit>0){
		$pagination.=" LIMIT  $start,$limit"; 
}
		$res=$_unite_email->SELECT($constraint,$pagination); 

		 $data=array();
		  while($row=$_unite_email->fetch_object()){
		  	$data[]=$row;
	  	  }
  	 	 
		
	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('totalcount' => count($data),'result' => $data ));
	  	
	  }}
?>